//
//  WeeklyLetterView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/21.
//

import SwiftUI

//每周来信
struct WeeklyLetterView: View {
    
    @StateObject var viewModel = WeeklyLetterViewModel()
    var body: some View {
        VStack(alignment: .leading,spacing : 0) {
            ZStack(alignment: .topTrailing) {
                VStack(alignment: .leading,spacing : 0) {
                    Text("每周来信")
                        .font(.custom(IFontType.custom.rawValue, size: 30))
                        .foregroundStyle(.bgLetter)
                        .fontWeight(.medium)
                    Text("每周一 6点-12点，AI朋友来信")
                        .font(.system(size: 14))
                        .foregroundStyle(.bgLetter)
                        .padding(.top, 6)
                }.frame(maxWidth: .infinity,alignment: .leading)
                    .padding(.top,20)
                Image(.appIconClose3)
                    .resizable()
                    .frame(width: 23, height: 23, alignment: .topTrailing)
                    .onTapGesture {
                        pathManager.back()
                    }
            }.padding(.leading, 30)
                .padding(.trailing,15)
            
            
            ZStack {
                if !viewModel.status.isEmpty {
                    HomeEmptyView(text1: viewModel.status)
                        .frame(maxWidth: .infinity,maxHeight: .infinity)
                } else {
                    ScrollView {
                        //需要嵌套一层LazyVStack，不然会产生间距，而且大小还会随机
                        LazyVStack(spacing: 0) {
                            ForEach(Array(viewModel.items.enumerated()), id: \.element.id) { index, item in
                                if (item.title).isEmpty == false {
                                    Text(String(item.title))
                                        .font(.custom(IFontType.number.rawValue, size: 24))
                                        .fontWeight(.bold)
                                        .foregroundStyle(Color.hex("#212121"))
                                        .padding(.top, 50)
                                } else {
                                    if let bean = item.items {
                                        WeeklyLetterItemView(item: bean)
                                            .contextMenu {
                                                Button("删除", role: .destructive) {
                                                    viewModel.doDel(index: index, bean: bean) {
                                                        withAnimation {
                                                            if !viewModel.items.isEmpty {
                                                                viewModel.items.remove(at: index)
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            .padding(.top, 20)
                                    }
                                }
                                
                                
                            }
                            LoadMoreFooter(refreshing: $viewModel.footerRefreshing, action: {
                                viewModel.getData(false)
                            }) {
                                LoadMoreView(noMore: viewModel.noMore, footerRefreshing: viewModel.footerRefreshing)
                            }
                            .noMore(viewModel.noMore)
                            .preload(offset: 10)
                        }
                    }
                    .enableRefresh()
                }
            }.frame(maxWidth: .infinity, maxHeight: .infinity)
                .padding(.horizontal,30)
                .padding(.top, 5)
        }.frame(maxWidth: .infinity,maxHeight: .infinity,alignment: .topLeading)
            .navigationTitle("")
            .toolbar(.hidden, for: .navigationBar)
            .background {
                ZStack(alignment: .topTrailing) {
                    Image(.homeBgLetter)
                        .resizable() // 允许图片调整尺寸
                        .scaledToFill() // 充满容器（超出部分裁剪）
                    
                    Image(.homeIconBgLetter)
                        .resizable()
                        .aspectRatio(contentMode: .fit) // 保持原始宽高比，完全显示图片（不裁剪）
                        .frame(width: 35)
                        .padding(.trailing, 48)
                        .padding(.top, 100)
                }.edgesIgnoringSafeArea(.all) // 覆盖安全区域
                    .padding(0) // 消除图片自身内边距
                
            }
            .onAppear{
                if !viewModel.isInit {
                    viewModel.getData(true)
                    viewModel.isInit = true
                }
            }
    }
}

#Preview {
    WeeklyLetterView()
}
